// pages/user/distribution.js
var sliderWidth = 40;
var app = getApp();
var common = require("../../utils/common.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
      winWidth: 0,
      winHeight: 0,
      // tab切换
      currentTab: 0,
      isStatus:'pay',//10待付款，20待发货，30待收货 40、50已完成
      page:0,
      refundpage:0,
      obligationList: [],//待付款
      sendOutList:[],//待发货
      receivingList:[],//待收货
      finishedList:[],//已完成
      refundList:[],//退款
      ordersNum: 0,//订单数量
      goodsTotalNum: 0,//商品总数
      goodsTotalPrice: 0,//总金额
      // 修改
      tabs: ["待付款", "待发货", "待收货", "已完成", "退款/售后"],
      activeIndex: 0,
      sliderOffset: 0,
      sliderLeft: 0,
      image: app.d.imageUrlFix,
      isRefresh: 0,//是否刷新
      //是否有数据
      isHaveData: {
        obIsHaveData: 0,//待付款是否有数据
        sendIsHaveData: 0,//待发货是否有数据
        receiveIsHaveData: 0,//待收货是否有数据
        finishIsHaveData: 0,//已完成是否有数
        refundIsHaveData: 0,//退款/售后是否有数据
      },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      var that = this;
      this.initSystemInfo();
      this.setData({
          currentTab: parseInt(options.currentTab),
          isStatus:options.otype,
          activeIndex: parseInt(options.activeIndex),
      });

      this.queryItemList();

      wx.getSystemInfo({
          success: function(res) {
              that.setData({
                  sliderLeft: (res.windowWidth / that.data.tabs.length - sliderWidth) / 4,
                  sliderOffset: res.windowWidth / that.data.tabs.length * that.data.activeIndex,
              });
          },
      });
  },

  initSystemInfo:function(){
    var that = this;
    wx.getSystemInfo( {
      success: function( res ) {
        that.setData( {
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }
    });
  },

  /**
 * 删除订单
 */
  delOrder: function (e) {
    var that = this;
    var orderId = e.currentTarget.dataset.orderid;
    wx.showModal({
      title: '提示',
      content: '您确定要删除订单吗？',
      success: function (res) {
        res.confirm && app.request("WxOrderListService", "modOrderStatus", { id: orderId, type: 'delete' }, function (data) {
          wx.showToast({
            title: '删除成功！',
            duration: 2000
          })
          that.queryItemList();
        });
      }
    });
  },

  /**
   * 查询列表
   */
  queryItemList: function () {
    if (this.data.activeIndex == 0) {
      this.queryObligationList();
    } else if (this.data.activeIndex == 1) {
      this.querySendOutList();
    } else if (this.data.activeIndex == 2) {
      this.queryReceivingList();
    } else if (this.data.activeIndex == 3) {
      this.queryFinishedList();
    } else if (this.data.activeIndex == 4) {
      this.queryRefundList();
    }
  },

  /**
   * 查询待付款列表
   */
  queryObligationList: function () {
    var that = this;
    var total = 0;
    var goodsTotalNum = 0;
    var goodsTotalPrice = 0;
    wx.showLoading({
      title: '数据加载中',
    });
    app.request("WxOrderListService", "queryDistributerOrder",
      { userId: app.d.userId, orderStatus: 0, shippingStatus: 0, payStatus: 0 }, function (data) {
        if (data.orderList != null && data.orderList.length > 0) {
          for (let i = 0; i < data.orderList.length; i++) {
            for (let j = 0; j < data.orderList[i].orderGoods.length; j++) {
              total += data.orderList[i].orderGoods[j].goods_number;
              goodsTotalNum += data.orderList[i].orderGoods[j].goods_number;
            }
            data.orderList[i]["totalAmount"] = total;
            total = 0;
            goodsTotalPrice += data.orderList[i].order_amount;
          }
        }
        that.setData({
          obligationList: data.orderList,
          ordersNum: data.orderList.length,
          goodsTotalNum: goodsTotalNum,
          goodsTotalPrice: goodsTotalPrice,
        });
        if (that.data.obligationList.length > 0) {
          that.setData({
            isHaveData: { obIsHaveData: 0 },
          });
        } else {
          that.setData({
            isHaveData: { obIsHaveData: 1 },
          });
        }
      }, function(data){

      }, function(data){
        wx.hideLoading();
      });
  },

  /**
   * 查询待发货列表
   */
  querySendOutList: function () {
    var that = this;
    var total = 0;
    var goodsTotalNum = 0;
    var goodsTotalPrice = 0;
    wx.showLoading({
      title: '数据加载中',
    })
    app.request("WxOrderListService", "queryDistributerOrder", {
      userId: app.d.userId, orderStatus: 0, shippingStatus: 0, payStatus: 2
    }, function (data) {
      if (data.orderList != null && data.orderList.length > 0) {
        for (let i = 0; i < data.orderList.length; i++) {
          for (let j = 0; j < data.orderList[i].orderGoods.length; j++) {
            total += data.orderList[i].orderGoods[j].goods_number;
            goodsTotalNum += data.orderList[i].orderGoods[j].goods_number;
          }
          data.orderList[i]["totalAmount"] = total;
          total = 0;
          goodsTotalPrice += data.orderList[i].order_amount;
        }
      }
      that.setData({
        sendOutList: data.orderList,
        ordersNum: data.orderList.length,
        goodsTotalNum: goodsTotalNum,
        goodsTotalPrice: goodsTotalPrice,
      });
      if (that.data.sendOutList.length > 0) {
        that.setData({
          isHaveData: { sendIsHaveData: 0 },
        });
      } else {
        that.setData({
          isHaveData: { sendIsHaveData: 1 },
        });
      }
      }, function (data) {

      }, function (data) {
        wx.hideLoading();
      });
  },

  /**
   * 查询待收获列表
   */
  queryReceivingList: function () {
    var that = this;
    var total = 0;
    var goodsTotalNum = 0;
    var goodsTotalPrice = 0;
    wx.showLoading({
      title: '数据加载中',
    });
    app.request("WxOrderListService", "queryDistributerOrder", {
      userId: app.d.userId, orderStatus: 0, shippingStatus: 1, payStatus: 2
    }, function (data) {
      if (data.orderList != null && data.orderList.length > 0) {
        for (let i = 0; i < data.orderList.length; i++) {
          for (let j = 0; j < data.orderList[i].orderGoods.length; j++) {
            total += data.orderList[i].orderGoods[j].goods_number;
            goodsTotalNum += data.orderList[i].orderGoods[j].goods_number;
          }
          data.orderList[i]["totalAmount"] = total;
          total = 0;
          goodsTotalPrice += data.orderList[i].order_amount;
        }
      }
      that.setData({
        receivingList: data.orderList,
        ordersNum: data.orderList.length,
        goodsTotalNum: goodsTotalNum,
        goodsTotalPrice: goodsTotalPrice,
      });
      if (that.data.receivingList.length > 0) {
        that.setData({
          isHaveData: { receiveIsHaveData: 0 },
        });
      } else {
        that.setData({
          isHaveData: { receiveIsHaveData: 1 },
        });
      }
    }, function (data) {

    }, function (data) {
      wx.hideLoading();
    });
  },

  /**
   * 查询已完成列表
   */
  queryFinishedList: function () {
    var that = this;
    var total = 0;
    var goodsTotalNum = 0;
    var goodsTotalPrice = 0;
    wx.showLoading({
      title: '数据加载中',
    });
    app.request("WxOrderListService", "queryDistributerOrder", {
      userId: app.d.userId, orderStatus: 1, shippingStatus: 2, payStatus: 2
    }, function (data) {
      if (data.orderList != null && data.orderList.length > 0) {
        for (let i = 0; i < data.orderList.length; i++) {
          for (let j = 0; j < data.orderList[i].orderGoods.length; j++) {
            total += data.orderList[i].orderGoods[j].goods_number;
            goodsTotalNum += data.orderList[i].orderGoods[j].goods_number;
          }
          data.orderList[i]["totalAmount"] = total;
          total = 0;
          goodsTotalPrice += data.orderList[i].order_amount;
        }
      }
      that.setData({
        finishedList: data.orderList,
        ordersNum: data.orderList.length,
        goodsTotalNum: goodsTotalNum,
        goodsTotalPrice: goodsTotalPrice,
      });
      if (that.data.finishedList.length > 0) {
        that.setData({
          isHaveData: { finishIsHaveData: 0 },
        });
      } else {
        that.setData({
          isHaveData: { finishIsHaveData: 1 },
        });
      }
    }, function (data) {

    }, function (data) {
      wx.hideLoading();
    });
  },

  /**
   * 查询退款列表
   */
  queryRefundList: function () {
    var that = this;
    var total = 0;
    var goodsTotalNum = 0;
    var goodsTotalPrice = 0;
    wx.showLoading({
      title: '数据加载中',
    });
    app.request("WxOrderListService", "queryDistributerOrder", {
      userId: app.d.userId, orderStatus: 4, shippingStatus: 4, payStatus: 2
    }, function (data) {
      if (data.orderList != null && data.orderList.length > 0) {
        for (let i = 0; i < data.orderList.length; i++) {
          for (let j = 0; j < data.orderList[i].orderGoods.length; j++) {
            total += data.orderList[i].orderGoods[j].goods_number;
            goodsTotalNum += data.orderList[i].orderGoods[j].goods_number;
          }
          data.orderList[i]["totalAmount"] = total;
          total = 0;
          goodsTotalPrice += data.orderList[i].order_amount;
        }
      }
      that.setData({
        refundList: data.orderList,
        ordersNum: data.orderList.length,
        goodsTotalNum: goodsTotalNum,
        goodsTotalPrice: goodsTotalPrice,
      });
      if (that.data.refundList.length > 0) {
        that.setData({
          isHaveData: { refundIsHaveData: 0 },
        });
      } else {
        that.setData({
          isHaveData: { refundIsHaveData: 1 },
        });
      }
    }, function (data) {

    }, function (data) {
      wx.hideLoading();
    });
  },

  /**
   * 点击导航栏
   */
  tabClick: function (e) {
    this.setData({
      sliderOffset: e.currentTarget.offsetLeft,
      activeIndex: e.currentTarget.id
    });
    this.queryItemList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    if (that.data.isRefresh == 1) {
      that.queryItemList();
      wx.showToast({
        title: '操作成功',
        duration: 2000,
      });
      that.setData({
        isRefresh:0,
      });
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
      var data = new Date();
      var shared = data.getTime()+"";
      var shareId = shared.substring(8, shared.length) + app.d.userId;
      return {
          title: '大肚商城',
          path: '/pages/index/index?duid=' + app.d.userId + "&shareId=" + shareId,
          success: function(res) {
              app.request("DistributorService", "shareLog", {
                  uid: app.d.userId,
                  shareId: shareId,
              }, function(data){
                  // 分享成功
                  console.log(res);
              })
          },
          fail: function(res) {
              // 分享失败
              console.log(res);
          }
      }
  }
})
